/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {boolean}
 */
var hasPathSum = function (root, targetSum) {
    if (root == null) return false
    let dp = []
    dp.push(root)
    while (true) {
        let temp = dp
        dp = []
        for (let node of temp) {
            if(node.left == null && node.right == null && node.val == targetSum) return true
            if (node.left) {
                node.left.val += node.val
                dp.push(node.left)
            }
            if (node.right) {
                node.right.val += node.val
                dp.push(node.right)
            }
        }
        // 队列为空时，结束循环
        if (dp.length == 0) break
    }
    return false
};